package com.skyworth.web.admin.store.model;

import com.skyworth.web.common.domain.AttachmentModel;
import com.skyworth.web.common.repository.AttachmentRepository;
import com.skyworth.web.admin.user.domain.UserModel;
import com.skyworth.web.admin.user.repository.UserRepository;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.persistence.*;

/**
 * Created by skyworth on 2016/8/19.
 */
@Entity
@Component
@Table(name="ff_store")
public class StoreModel {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public long id;

	@Column(name="company_id")
	public long companyId;

	public String name;

	@Column(name="logo_id")
	public long logoId;

	public String description;

	@Column(name="owner_id")
	public long ownerId;

	@Column(name="remove_flag")
	public int removeFlag;

	@Transient
	private static AttachmentRepository attachmentRepository;

	@Autowired
	public void setAttachmentRepository(AttachmentRepository attachmentRepository) {
		StoreModel.attachmentRepository = attachmentRepository;
	}

	@Transient
	private static UserRepository userRepository;

	@Autowired
	public void setUserRepository(UserRepository userRepository) {
		StoreModel.userRepository = userRepository;
	}

	public String getOwner(){
		UserModel userModel = StoreModel.userRepository.findOne(this.ownerId);
		if(null != userModel){
			return userModel.getUsername();
		}else {
			return "SkyWorth";
		}
	}

	public String getLogoUrl() {
		AttachmentModel item = StoreModel.attachmentRepository.findOne(this.logoId);
		if (null == item) {
			return null;
		}
		return item.url;
	}

	public long getOwnerId() {
		return ownerId;
	}

	public void setOwnerId(long ownerId) {
		this.ownerId = ownerId;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getCompanyId() {
		return companyId;
	}

	public void setCompanyId(long companyId) {
		this.companyId = companyId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public long getLogoId() {
		return logoId;
	}

	public void setLogoId(long logoId) {
		this.logoId = logoId;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public int getRemoveFlag() {
		return removeFlag;
	}

	public void setRemoveFlag(int removeFlag) {
		this.removeFlag = removeFlag;
	}
}
